<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\AuthGroup as GroupModel;
use app\admin\model\Rules as RuleModel;

class Group extends Base
{
    //用户组列表首页的渲染
    public function group(){
        return view();
    }
    //用户组列表首页数据接口
    public function group_List(){
        $model = new GroupModel();
        $page = input('get.page');
        $limit = input('get.limit');
        $group = $model->limit(($page - 1) * $limit, $limit)->select();
        $data = [];
        foreach($group as$key => $v){
            $data[$key] = $v->toArray();
            $id = $v->rules;
            $rule =Db :: query("select `title` as name from `think_auth_rule` where `id` in ($id) ");
            $str = "";
            foreach( $rule as $v){
                $str .= $v["name"].",";
            }
            $data[$key]['name'] = $str;
        }

        $count = $model->count();
        return json(['code' => 0, 'msg' => "", 'count' => $count, 'data' => $data]);
    }
    //用户组以及关联表数据删除
    public function delGroup(){
        $id = input('post.id');
        $model = new GroupModel();
        $user = $model->find($id);
        $k = [];
        foreach ($user->authUser as $key => $v) {
            $k[] = $v->id;
        }
        if(!empty($k)) {
            $user->authUser()->detach($k);
        }

        $res = $model->where('id',$id)->delete();
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '删除失败']);
        }
    }
    //用户组编辑页面渲染
    public function groupUpd(){
        $model = new RuleModel;
        $id = input('get.id');
        $rules = Db::name('AuthRule')->select();
        $data = Db::name('AuthGroup')->where('id',$id)->find();
        $data['rules'] = explode(',',$data['rules']);
        $list = $model->getRules($rules,0);
        $this->assign('data',$data);
        $this->assign('rules',$list);
        return view();
    }
    //用户组编辑数据处理
    public function groupUpdateDo(){
        $data = input('post.');
        $group = implode(',',$data["rules"]);
        $data["rules"] = $group;
        $model = new GroupModel();
        $res = $model->isUpdate(true)->allowField(true)->save($data);
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2,'msg' => '删除失败']);
        }
    }
    //添加用户组页面渲染
    public function groupAdd(){
        $model = new RuleModel;
        $rules = Db::name('AuthRule')->select();
        $list = $model->getRules($rules,0);
        $this->assign('rules',$list);
        return  view();
    }
    //添加用户组数据处理
    public function groupAddDo(){
        $data = input('post.');
        $data['rules'] = implode(',',$data["rules"]);
        if(isset($data['status'])){
            $data['status']=1;
        }else{
            $data['status']=0;
        }
        $model = new GroupModel();
        $res = $model->allowField(true)->save($data);
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '添加失败']);
        }
    }

    //用户组状态修改数据表处理
    public function groupUpdateStatus(){
        $id = input('post.id');
        $data = GroupModel::get($id);
        if(input('post.status') == 'true'){
            $data->status = 1;
        }else{
            $data->status = 0;
        }
        $res = $data->allowField(true)->save();
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '修改失败']);
        }
    }
    //用户组修改规则  规则树形菜单请求处理
    public function getRulesData(){
        $model = new GroupModel();
        $data = Db::name('auth_rule')->where('status',1)->select();
        $groupRules = $model->getRules($data,0);
        return $groupRules;
    }
}
